package com.ruoyi.report_jkdj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_jkdj.domain.DjczljlBaDetail;
import com.ruoyi.report_jkdj.domain.vo.DjczljlBaDetailVo;
import com.ruoyi.report_jkdj.mapper.DjczljlBaDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_jkdj.domain.bo.DjczljlBsInfoBo;
import com.ruoyi.report_jkdj.domain.vo.DjczljlBsInfoVo;
import com.ruoyi.report_jkdj.domain.DjczljlBsInfo;
import com.ruoyi.report_jkdj.mapper.DjczljlBsInfoMapper;
import com.ruoyi.report_jkdj.service.IDjczljlBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 地基承载力试验检测记录Service业务层处理
 *
 * @author zyb
 * @date 2024-07-25
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class DjczljlBsInfoServiceImpl implements IDjczljlBsInfoService {

    private final DjczljlBsInfoMapper baseMapper;
    private final DjczljlBaDetailMapper detailMapper;

    /**
     * 查询地基承载力试验检测记录
     */
    @Override
    @FIllBasicInfo
    public DjczljlBsInfoVo queryById(Long excelId){
        DjczljlBsInfoVo djczljlBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<DjczljlBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<DjczljlBaDetail>()
            .eq(DjczljlBaDetail::getExcelId, excelId)
            .orderByAsc(DjczljlBaDetail::getXuh));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            djczljlBsInfoVo.setBaDetailVos(baDetailVos);
        }
        return djczljlBsInfoVo;
    }

    /**
     * 查询地基承载力试验检测记录列表
     */
    @Override
    public TableDataInfo<DjczljlBsInfoVo> queryPageList(DjczljlBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DjczljlBsInfo> lqw = buildQueryWrapper(bo);
        Page<DjczljlBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询地基承载力试验检测记录列表
     */
    @Override
    public List<DjczljlBsInfoVo> queryList(DjczljlBsInfoBo bo) {
        LambdaQueryWrapper<DjczljlBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DjczljlBsInfo> buildQueryWrapper(DjczljlBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DjczljlBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, DjczljlBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, DjczljlBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), DjczljlBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), DjczljlBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), DjczljlBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), DjczljlBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), DjczljlBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), DjczljlBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), DjczljlBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), DjczljlBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), DjczljlBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), DjczljlBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), DjczljlBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), DjczljlBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), DjczljlBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), DjczljlBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), DjczljlBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), DjczljlBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), DjczljlBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), DjczljlBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), DjczljlBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(bo.getShejczl() != null, DjczljlBsInfo::getShejczl, bo.getShejczl());
        lqw.eq(StringUtils.isNotBlank(bo.getJisgs()), DjczljlBsInfo::getJisgs, bo.getJisgs());
        lqw.eq(bo.getJisgsK() != null, DjczljlBsInfo::getJisgsK, bo.getJisgsK());
        lqw.eq(bo.getJisgsB() != null, DjczljlBsInfo::getJisgsB, bo.getJisgsB());
        lqw.eq(StringUtils.isNotBlank(bo.getImageUrl()), DjczljlBsInfo::getImageUrl, bo.getImageUrl());
        return lqw;
    }

    /**
     * 新增地基承载力试验检测记录
     */
    @Override
    public Boolean insertByBo(DjczljlBsInfoBo bo) {
        DjczljlBsInfo add = BeanUtil.toBean(bo, DjczljlBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 10
            ArrayList<DjczljlBaDetail> details = new ArrayList<>(10);
            for (long i = 1; i <= 10; i++) {
                details.add(new DjczljlBaDetail(add.getExcelId(), i));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改地基承载力试验检测记录
     */
    @Override
    public Boolean updateByBo(DjczljlBsInfoBo bo) {
        DjczljlBsInfo update = BeanUtil.toBean(bo, DjczljlBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), DjczljlBaDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DjczljlBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除地基承载力试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
